<?php

namespace common\service;


use common\model\RoleModel;

class RoleService
{
    /**
     * 获取角色列表
     * @param $param
     * @return array
     */
    public function select($param)
    {
        $roleModel = model(RoleModel::class);

        $options = [
            "order" => 'role.created desc',
            "fields" => 'role.id,role.name,role.code,role.type,role.created,role.is_platform,user.name'
        ];

        if (array_key_exists('filter', $param)) {
            $options['filter'] = $param['filter'];
        }

        if (array_key_exists('page', $param)) {
            $options['page'] = $param['page'];
        }

        $tenantData = $roleModel->selectData($options);

        $formatTenantData = [];
        if (!empty($tenantData['rows']) && $tenantData['total'] > 0) {
            foreach ($tenantData['rows'] as $tenantItem) {
                $tempData = $tenantItem['role'];
                $tempData['created_by'] = $tenantItem['user']['name'];
                $formatTenantData[] = $tempData;
            }
        }

        $resData = [
            'total' => $tenantData['total'],
            'rows' => $formatTenantData
        ];

        return $resData;
    }
}